﻿
//***********************************************************************
// * Module:   TbBstCrimecluesHandler
// * 作者:  曹荣芝
// * Modified: 2014/6/14
// * Purpose: Definition of the Class TbBstCrimecluesHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
using System.Collections;
using JWBSTAPP.CommonClass;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstCrimecluesHandler 的摘要说明
    /// </summary>
    public class TbBstCrimecluesHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstCrimeclues mTbBstCrimeclues = new TbBstCrimeclues();
                mTbBstCrimeclues.Id = Guid.NewGuid().ToString(); //主键
                mTbBstCrimeclues.Name = context.Request.Form["texName"]; //姓名（必填）
                mTbBstCrimeclues.Tel = context.Request.Form["texTel"]; //手机号码（必填）
                mTbBstCrimeclues.Email = context.Request.Form["texEmail"]; //电子邮箱
                mTbBstCrimeclues.Address = context.Request.Form["texAddress"]; //联系地址
                mTbBstCrimeclues.Content = context.Request.Form["texContent"]; //举报内容（必填）
                mTbBstCrimeclues.Ipaddress = context.Request.Form["texIpaddress"]; //ip地址
                mTbBstCrimeclues.Movesign = context.Request.Form["texMovesign"]; //同步标志
                mTbBstCrimeclues.Status = context.Request.Form["texStatus"]; //状态  0  未推送 1 已推送

                mResultModel = TbBstCrimecluesBLL.Insert(mTbBstCrimeclues, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstCrimeclues mTbBstCrimeclues = new TbBstCrimeclues();
                foreach (var paramKey in arryParamKey)
                {
                    var Crimeclues = TbBstCrimecluesBLL.Load(paramKey);
                    if (Crimeclues.Status == "0")
                    {
                        mResultModel = TbBstCrimecluesBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    }
                    else
                    {
                        mResultModel.ErrorCode = -7777;
                        mResultModel.ISSucceed = false;
                        mResultModel.Message = "无法删除已审核的信息";
                    }
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstCrimeclues mTbBstCrimeclues = new TbBstCrimeclues();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstCrimeclues = TbBstCrimecluesBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstCrimeclues != null)
                {
                    mTbBstCrimeclues.Name = context.Request.Form["texName"]; //姓名（必填）
                    mTbBstCrimeclues.Tel = context.Request.Form["texTel"]; //手机号码（必填）
                    mTbBstCrimeclues.Email = context.Request.Form["texEmail"]; //电子邮箱
                    mTbBstCrimeclues.Address = context.Request.Form["texAddress"]; //联系地址
                    mTbBstCrimeclues.Content = context.Request.Form["texContent"]; //举报内容（必填）
                    mTbBstCrimeclues.Ipaddress = context.Request.Form["texIpaddress"]; //ip地址
                    mTbBstCrimeclues.Movesign = context.Request.Form["texMovesign"]; //同步标志
                    mTbBstCrimeclues.Status = context.Request.Form["texStatus"]; //状态  0  未推送 1 已推送

                    mResultModel = TbBstCrimecluesBLL.Update(mTbBstCrimeclues, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            Hashtable ht = new Hashtable();
            string json = "";
            try
            {
                TbBstCrimeclues mTbBstCrimeclues = new TbBstCrimeclues(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstCrimeclues = TbBstCrimecluesBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstCrimeclues != null)
                    {
                        json = JsonConvert.SerializeObject(mTbBstCrimeclues);
                    }

                    if (!string.IsNullOrEmpty(mTbBstCrimeclues.Status))
                    {
                        string stmes = "未审核";
                        if (mTbBstCrimeclues.Status == "1")
                        {
                            stmes = "已审核";
                        }
                        ht.Add("Status", stmes);
                    }
                    json = JsonString.AddAttribute(json, ht);
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstCrimeclues = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            IsoDateTimeConverter dtConverter;
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            string name = context.Request.Form["texName"];
            string Tel = context.Request.Form["texTel"];
            string SHZT = context.Request.Form["selStatus"];
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstCrimecluesConst.Columns.Name, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(Tel))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstCrimecluesConst.Columns.Tel, '%' + Tel + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(SHZT))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstCrimecluesConst.Columns.Status, SHZT), SqlLogicOper.AND_LOGIC);
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_EQUAL, TbBstCrimecluesConst.Columns.Status, "2"), SqlLogicOper.AND_LOGIC);
            dtTbBstCrimeclues = Q.GetRecordByPage("*", sqllist.ToSql(), "CREATEDATE", intPageSize, intPageIndex, TbBstCrimecluesConst.TableName, TbBstCrimecluesConst.FieldKey, "DESC").Tables[0];
            if (dtTbBstCrimeclues != null && dtTbBstCrimeclues.Rows.Count > 0)
            {

                for (int i = 0; i < dtTbBstCrimeclues.Rows.Count; i++)
                {
                    string mes = "未审核";
                    string color = "FF0000";
                    if (dtTbBstCrimeclues.Rows[i]["STATUS"].ToString() == "1")
                    {
                        //已审核
                        mes = "已审核";
                        color = "32C904";
                    }
                    dtTbBstCrimeclues.Rows[i]["STATUS"] = "<span style=\"color:#" + color + "\">" + mes + "</span>";
                }
                for (int i = 0; i < dtTbBstCrimeclues.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstCrimeclues.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstCrimeclues;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm,dtConverter);
            context.Response.Write(json);
        }
        #endregion


        //审核
        public void ToExamine()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                foreach (var paramKey in arryParamKey)
                {
                    var TbBstCrimeclues = TbBstCrimecluesBLL.Load(paramKey);
                    if (TbBstCrimeclues != null)
                    {
                        TbBstCrimeclues.Status = "1";
                        TbBstCrimeclues.Movesign = "1";
                        mResultModel = TbBstCrimecluesBLL.Update(TbBstCrimeclues, "", false);
                    }
                    else
                    {
                        mResultModel.ErrorCode = -9990;
                        mResultModel.ISSucceed = false;
                    }
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel));
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
    }
}


